/*
 * CPthreadI_test.cpp
 *
 *  Created on: 2013-6-19
 *      Author: wangrongming
 */

#include "CPthreadI.h"

#include "CTraceLog.h"

void * pt_func(void * param)
{

	__DebugLog0("Enetr into pt_func ptid=[%lu] param=[%p]\n", pthread_self(), param);

	ptid_t ptid = pthread_self();

	PtParam_t * prPtParam = (PtParam_t *)param;
	if(NULL == prPtParam)
	{
		__DebugLog0("[pt_func] ptid=[%lu] PtParam_t Pointer is null. \n ", ptid);

		return NULL;
	}

	CPthread_I * pThis =  prPtParam->rtPthObj;
	if(NULL == pThis)
	{
		__DebugLog0("[pt_func]  ptid=[%lu] CPthread_I Pointer is null. \n ", ptid);

		return NULL;
	}

	int nSleep = 0x02;
	__DebugLog3("[pt_func]  run_start ptid=[%lu] nSleep(%d) param=[%p]\n", pthread_self(), nSleep, param);

	sleep( nSleep );

	__DebugLog3("[pt_func]  run_end   ptid=[%lu] sleep(%d) \n", pthread_self(), nSleep);

	return NULL;
}

int test_Pthread_I()
{
//	pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//	pthread_cond_t cond   = PTHREAD_COND_INITIALIZER;

	CTraceLog dLog("CPthreadI.log", 0x00);

	ptid_t owner_ptid = pthread_self();
	funcx_t func = pt_func;

	__DebugLog5("stap01 create pthObj\n");

	int nRet = 0x00;
	CPthread_I pthObj;
	PtParam_t m_tPtParam;

	m_tPtParam.nMgrVal = 0x01;
	m_tPtParam.ptr = NULL;
	m_tPtParam.rtMgr = NULL;
	m_tPtParam.rtPthObj = &pthObj;

	__DebugLog5("stap01  pthObj.init\n");
	//pthObj.init(owner_ptid, & mutex, & cond, &dLog);
	pthObj.init(owner_ptid, NULL, NULL, &dLog);

	__DebugLog5("stap02  pthObj.create\n");
	nRet = pthObj.create(func, &m_tPtParam);

	if(nRet < 0x00)
	{
		__DebugLog5("stap02  pthObj.create Failed. nRet= %d \n", nRet);
		return nRet;
	}

	__DebugLog5("stap03 pthObj.start\n");


	for(int i = 0 ; i< 10; i++)
	{
		__DebugLog5("stap04 pthObj.start\n");
		sleep(2);
		pthObj.start();
	}


	__DebugLog5("stap05  detach begin\n");
	pthObj.wait2stop(NULL);
	__DebugLog5("stap05  detach end\n");


	return 0;
}


int test_CPthread_Pool_Manager(int nPoolSize )
{

	nPoolSize = nPoolSize> 0x00? nPoolSize: 0x01;

//	pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//	pthread_cond_t cond   = PTHREAD_COND_INITIALIZER;

	static CTraceLog dLog("CPthreadI.log", 0x00);

	ptid_t owner_ptid = pthread_self();
	funcx_t func = pt_func;

	__DebugLog5("stap01 create pthObj\n");

	int nRet = 0x00;

	__DebugLog5("stap02 create tMgr\n");
	//static  CPthread_Pool_Manager_Tow tMgr ;
	  CPthread_Pool_Manager_Tow tMgr ;

	__DebugLog5("stap03 create tMgr.init\n");
	tMgr.init(nPoolSize, NULL, NULL, &dLog);

	__DebugLog5("stap04 create tMgr.CreatePool\n");
	nRet = tMgr.CreatePool(nPoolSize);

	CPthread_I *  pthObj = NULL;

//	pthObj = new CPthread_I();
//	pthObj->setdebuglog(&dLog);
//	pthObj->setfunc(pt_func, &tMgr.m_tPtParam);
//	tMgr.vFeed_Free(pthObj);

	static CTask_I oTask;
	oTask.init(0x00, &dLog);

	int nSizeFree = tMgr.vSizeFree();
	nSizeFree = nSizeFree < 0x01? 0x01:nSizeFree;

	__DebugLog5("stap05 create tMgr.vUsed_Free.size=%d\n", nSizeFree);

//	for(int i = 0x00; i< nSizeFree-2; i++)
//	{
//		pthObj =  tMgr.vGetPthread();
//
//		if(NULL != pthObj)
//		{
//			__DebugLog5("stap06 start tMgr.vUsed_Free.size=%d pthObj->start[%d th] pthObj=%p \n",
//					tMgr.vSizeFree(),i+0x01, pthObj);
//
//			//tMgr.vFeed_Work(pthObj);
//
//			pthObj->vSetTask(&oTask, NULL);
//			pthObj->start();
//
//			__DebugLog5("stap06 start tMgr.vFeed_Work.size=%d pthObj->start[%d th] pthObj=%p self_ptid=[%lu]\n",
//					tMgr.vSizeWork(), i+0x01, pthObj, pthObj->getselfptid());
//
//		}else
//		{
//			__DebugLog5("stap06 start tMgr.vUsed_Free pthObj is null [%d th]\n", i+0x01);
//		}
//	}


	pthObj =  tMgr.vGetPthread();
	if(pthObj)
	{
		__DebugLog5("stap066 start tMgr.vUsed_Free.size=%d pthObj->start[%d th] pthObj=%p \n",
				tMgr.vSizeFree(),0x01, pthObj);

		//tMgr.vFeed_Work(pthObj);

		pthObj->vSetTask(&oTask, NULL);
		pthObj->start();

		sleep(4);

		pthObj->wait2suspend();

		pthObj->resume();
		__DebugLog5("stap066 start tMgr.vFeed_Work.size=%d pthObj->start[%d th] pthObj=%p self_ptid=[%lu]\n",
				tMgr.vSizeWork(), 0x01, pthObj, pthObj->getselfptid());
	}



//	tMgr.lock();
//
//	int nSizeWork = tMgr.vSizeWork();
//	//nSizeWork = nSizeWork < 0x01? 0x01:nSizeWork;
//
//	__DebugLog5("stap07 waitstop tMgr.vUsed_Work.size=%d\n", nSizeWork);
//	for(int i = 0x00; i< nSizeWork -1; i++)
//	{
//		pthObj =  tMgr.vUsed_Work();
//		if(NULL != pthObj)
//		{
//			__DebugLog5("stap07  tMgr.vUsed_Work.size=%d pthObj->waitstop[%d th] Begin pthObj=%p\n",
//					tMgr.vSizeWork(), i+0x01, pthObj);
//
//
//			pthObj->waitstop();
//
//
//			__DebugLog5("stap07  tMgr.vUsed_Work.size=%d pthObj->waitstop[%d th] End pthObj=%p\n",
//					tMgr.vSizeWork(), i+0x01, pthObj);
//
//			//tMgr.vFeed_Free(pthObj);
//			delete pthObj;pthObj = NULL;
//
//			__DebugLog5("stap07  tMgr.vUsed_Work.size=%d pthObj->waitstop[%d th] End pthObj=%p\n",
//						tMgr.vSizeWork(), i+0x01, pthObj);
//
//
//		}else
//		{
//			__DebugLog5("stap07  tMgr.vUsed_Work pthObj is null [%d th]\n", i+0x01);
//		}
//	}
//
//	tMgr.unlock();
	//sleep(20);

//	__DebugLog5("stap08  CPthread_Pool_Manager_Tow::lock_begin vSizeFree=%d \n", tMgr.vSizeFree());
//
//	tMgr.lock();
//
//	__DebugLog5("stap08  CPthread_Pool_Manager_Tow::lock_end vSizeFree=%d \n", tMgr.vSizeFree());
//
//	tMgr.vClearupFree();
//	__DebugLog5("stap08  tMgr.vClearupFree end\n");
//
//	__DebugLog5("stap09  tMgr.vClearupWork begin vSizeFree=%d \n", tMgr.vSizeWork());
//	tMgr.vClearupWork();
//	__DebugLog5("stap09  tMgr.vClearupWork end\n");
//
//	tMgr.unlock();
//
//	__DebugLog5("stap09  unlock Mgr-lock end \n");


	return 0;
}
